package com.drivers.wallet.service.impl;

import cn.hutool.core.date.DateTime;
import com.drivers.common.wallet.BankCard;
import com.drivers.common.wallet.BankCards;
import com.drivers.model.BaseEntity;
import com.drivers.model.wallet.DriversWallet;
import com.drivers.model.wallet.ResultData;
import com.drivers.wallet.mapper.WalletMapper;
import com.drivers.wallet.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 钱包模块
 * @Author zhaomingshuo
 * @Date: 2024/08/26
 */


@Service
public class WalletServiceImpl  implements WalletService {
    @Autowired
    WalletMapper walletMapper;

    /**
     *
     * 查询所有银行卡
     * @return
     */
    @Override
    public List<DriversWallet> walletfindAll(Integer userid) {
        return walletMapper.walletfindAll(userid);
    }



    /**
     *
     * 添加新的银行卡
     * 添加成功返回1 添加失败返回0
     * @param driversWallet
     * @return
     */

    @Override
    public Integer walletadd(DriversWallet driversWallet) {
//        枚举类型的获取银行卡信息
        /*
                try {
//        校验银行卡是否正确
            if(!BankCard.checkBankCard(driversWallet.getBankCardNumber())){
                return 2;
            }
            String nameOfBank = BankCard.getNameOfBank(driversWallet.getBankCardNumber());
            if (nameOfBank==null){
                return 2;
            }
            driversWallet.setBankName(nameOfBank);
        } catch (Exception e) {
            return 0;
        }
         */
        if (null==driversWallet.getBankCardNumber() || driversWallet.getBankCardNumber()=="") {
            return 3;
        }



        try {
            ResultData resultData = BankCards.checkBankCard(driversWallet.getBankCardNumber());
            //        示例
//        System.out.println("Request ID: " + resultData.getRequest_id());
//        System.out.println("Bank: " + resultData.getResult().getBank());
//            {"request_id":"MTIDb6596b86f02c4d1cba3e1e56b0f3567c","status":"OK","result_code":0,"result_message":"查询成功",
//            "result":{"bank":"北京银行","cardtype":"借记卡","nature":"京卡","kefu":"010-96169","bankcard":"6214686362041837",
//            "logo":"http://bkaear.market.alicloudapi.com/banklogo/BeijingYinHang.gif","info":"北京-北京"}}
            if (resultData.getStatus()==null) {
                return 0;
            }
//            resultData
//            resultData
//            名称
            driversWallet.setBankName(resultData.getResult().getBank());
//            头像
            driversWallet.setBankIcon(resultData.getResult().getLogo());

        } catch (Exception e) {
            return 2;
        }
        driversWallet.setIsDelete(0);
        driversWallet.setPrecedenceStatus(0);
        driversWallet.setPasswordFreeStatus(0);
        driversWallet.setBindStatus(1);
        driversWallet.setCreateTime(LocalDateTime.now());
//        银行卡图片
//        driversWallet.setBankIcon("http://qiniutest.zhiyuzhuli.com/db702f661281412eba894f3a9dc6e018.png");

        Integer walletadd = walletMapper.walletadd(driversWallet);
        if (walletadd > 0){
            return walletadd;
        }
        return  0;
    }

    /**
     *修改用户的银行卡免密状态
     * 关闭免密支付 免密状态(已开通:1;去开通:0)
     * @param id
     * @return
     */
    @Override
    public Integer updateFreeStatus(Integer id) {
        if (id==null){
            return 0;
        }

        LocalDateTime now = LocalDateTime.now();
        Integer i =walletMapper.updateFreeStatus(id,now);
        if (i>0){
            return i;
        }
        return 0;
    }

    /**
     *修改用户的银行卡优先级状态
     * @param id
     * @return
     */
    @Override
    public Integer precedenceStatus(Integer id) {
        if (id==null){
            return 0;
        }
        LocalDateTime now = LocalDateTime.now();
        walletMapper.precedencecancel(now);

        Integer i =walletMapper.precedenceStatus(id,now);
        if (i>0){
            return i;
        }
        return 0;
    }

    @Override
    public Integer openSecretfree(Integer id) {
        LocalDateTime now = LocalDateTime.now();
        Integer i =walletMapper.openSecretfree(id,now);
        if (i>0){
            return i;
        }
        return 0;
    }

    /**
     *
     * 根据id查询银行卡1
     * @param id
     * @return
     */
    @Override
    public DriversWallet DriversWalletbyid(Integer id) {
        return walletMapper.DriversWalletbyid(id);
    }

    /**
     *
     * 逻辑删除银行卡
     * @param id
     * @return
     */
    @Override
    public Integer deletewallet(Integer id) {
        return walletMapper.deletewallet(id);
    }


}
